<?php

namespace App\Admin\Controllers;

use App\Admin\Extensions\StatistcsExcelExpoter;
use App\Models\WorkUser;
use Dcat\Admin\Admin;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use App\Admin\Repositories\WorkUserStatisticsApi;
use Dcat\Admin\Http\Controllers\AdminController;
use Illuminate\Support\Facades\DB;

class WorkUserStatisticsController extends AdminController
{

    protected $title = '务工人员信息统计(天)';

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid($repository = null)
    {
        $grid = new Grid(new WorkUserStatisticsApi());
        $grid->disableCreateButton();

        $grid->filter(function (Grid\Filter $filter) {
            $filter->panel();
            $filter->expand();

//            $filter->between('start', '时间范围')->datetime()->width(5);
            $filter->date('start', '开始时间')->width(5);
            $filter->date('end', '结束时间')->width(5);

            $filter->like('engineering', '所属工程')->select(function ($status){
                $Adminid  = Admin::user()->id;
                if($Adminid != 1){
                    $engineering_name = DB::table('engineering')->where('manager','like','%'.$Adminid.'%')->where('deleted_at',null)->pluck('engineering_name');
                }else{
                    $engineering_name = DB::table('engineering')->where('deleted_at',null)->pluck('engineering_name');
                }
                $arr = [];
                foreach ($engineering_name as $key => $value){
                    $arr["$value"] = $value;
                }
                return $arr;
            })->width(5);
        });
        $grid->disablePagination();
        $grid->export(new StatistcsExcelExpoter());

        $grid->column('engineering','所属工程')->limit(10);
        $grid->column('time','时间');
        $grid->column('type','行为')->display(function ($type) {
                if($type == 1){
                    return "<span style='color:green'>入职</span>";
                }
                if($type == 2){
                    return "<span style='color:red'>离职</span>";
                }
                if($type == 3){
                    return "<span style='color:green'>再入职</span>";
                }
                if($type == 4){
                    return "<span style='color:red'>劣迹</span>";
                }
        });
        $grid->column('num','人数');

//        $grid->column('job_num','在职总人数');
//        $grid->column('resign_num','离职总人数');
//        $grid->column('misdeed_num','劣迹总人数');


        $grid->footer(function ($collection) use ($grid) {
            $engineering = $grid->model()->filter()->input('engineering');

            // 查出统计数据
            $job_num= DB::table('work_user')->where('engineering',$engineering)
                    ->where('status',1)->where('misdeed',1)
                    ->count();
            $resign_num = DB::table('work_user')->where('engineering',$engineering)
                ->where('status',2)->where('misdeed',1)
                ->count();
            $misdeed_num = DB::table('work_user')->where('engineering',$engineering)
                ->where('misdeed','<>',1)
                ->count();



            return "<button class='btn btn-primary btn-light shadow-none' data-toggle='dropdown'>在职总人数:$job_num</button>
                    <button class='btn btn-primary btn-light shadow-none' data-toggle='dropdown'>离职总人数:$resign_num</button>
                    <button class='btn btn-primary btn-light shadow-none' data-toggle='dropdown'>劣迹总人数:$misdeed_num</button>";
        });



        $grid->disableActions();
        $grid->actions(function (Grid\Displayers\Actions $actions) {
            $actions->disableDelete();
            $actions->disableEdit();
            $actions->disableQuickEdit();
            $actions->disableView();
        });


        return $grid;
    }

    protected function form()
    {
        $form = new Form(new InTheater());

        $form->display('id', 'ID');
        $form->text('title')->rules('required');
        $form->text('original_title');
        $form->textarea('summary');
        $form->url('alt');
        $form->url('mobile_url');
        $form->url('share_url');
        $form->tags('countries');
        $form->tags('genres');
        $form->tags('aka');
        $form->year('year');

        $form->disableViewButton();
        $form->disableViewCheck();

        return $form;
    }
}
